﻿using System.IO;
using NUnit.Framework;

namespace Luna.Data.SqlCe.Test
{
    public class ManagerTest : AssertionHelper
    {
        private const string TEST_FILE = "Test.lunadata";
        private const string TEST_FILE_2 = "Test2.lunadata";
        private const string NOT_FOUND = "xxx.lunadata";
        private SqlCeDatabaseManager _Manager;

        [SetUp]
        public void SetUp()
        {
            _Manager = new SqlCeDatabaseManager();
            _Manager.Current = new LunaDatabaseAdapter();
        }

        [Test]
        public void TestCreate()
        {
            _Manager.Create(TEST_FILE);
            Expect(File.Exists(TEST_FILE));
            LunaDatabaseAdapter L_Adapter = _Manager.Current as LunaDatabaseAdapter;
            Expect(L_Adapter.FileName == TEST_FILE);
        }

        [Test]
        public void TestOpen()
        {
            _Manager.Create(TEST_FILE);
            _Manager.Open(TEST_FILE);
            LunaDatabaseAdapter L_Adapter = _Manager.Current as LunaDatabaseAdapter;
            Expect(L_Adapter.FileName == TEST_FILE);
        }

        [Test]
        public void TestOpenFailed()
        {
            Expect(() => _Manager.Open(NOT_FOUND), Throws.Exception);
        }

        [Test]
        public void TestSaveFailed()
        {
            Expect(() => _Manager.SaveAs(Path.GetTempFileName()), Throws.Exception);
        }

        [Test]
        public void TestSaveAs()
        {
            _Manager.Create(TEST_FILE);
            _Manager.SaveAs(TEST_FILE_2);
            Expect(File.Exists(TEST_FILE_2));
            LunaDatabaseAdapter L_Adapter = _Manager.Current as LunaDatabaseAdapter;
            Expect(L_Adapter.FileName == TEST_FILE_2);
        }

        [TearDown]
        public void TearDown()
        {
            if (File.Exists(TEST_FILE))
            {
                File.Delete(TEST_FILE);
            }

            if (File.Exists(TEST_FILE_2))
            {
                File.Delete(TEST_FILE_2);
            }
        }
    }
}
